package yue.shen.android.demo.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class RViewAdapter<T> extends RecyclerView.Adapter<RViewHolder> {

    private ItemListener<T> itemListener;
    private final List<T> dataList;
    private final RViewItemManager<T> itemManager;

    public RViewAdapter(List<T> dataList) {
        this(dataList, null);
    }

    public RViewAdapter(List<T> dataList, RViewItem<T> item) {
        if (dataList == null) {
            this.dataList = new ArrayList<>();
        } else {
            this.dataList = dataList;
        }
        itemManager = new RViewItemManager<>();
        if (item != null) {
            addItemStyle(item);
        }
    }

    public void setItemListener(ItemListener<T> itemListener) {
        this.itemListener = itemListener;
    }

    public void addItemStyle(RViewItem<T> item) {
        itemManager.addItemStyle(item);
    }

    private boolean hasMultiStyle() {
        return itemManager.getItemViewStyleCount() > 1;
    }

    @NonNull
    @Override
    public RViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RViewItem<T> item = itemManager.getRViewItem(viewType);
        int layoutId = item.getItemLayout();
        RViewHolder viewHolder = RViewHolder.createViewHolder(parent.getContext(), parent, layoutId);
        if (item.openClick()) {
            setListener(viewHolder);
        }
        return viewHolder;
    }

    private void setListener(RViewHolder viewHolder) {
        View view = viewHolder.getConvertView();
        view.setOnClickListener(v -> {
            if (itemListener != null) {
                int position = viewHolder.getAdapterPosition();
                itemListener.onItemClick(v, dataList.get(position), position);
            }
        });

        view.setOnLongClickListener(v -> {
            if (itemListener != null) {
                int position = viewHolder.getAdapterPosition();
                return itemListener.onItemLongClick(v, dataList.get(position), position);
            }
            return false;
        });
    }

    @Override
    public void onBindViewHolder(@NonNull RViewHolder holder, int position) {
        convert(holder, dataList.get(position));
    }

    private void convert(RViewHolder holder, T entity) {
        itemManager.convert(holder, entity, holder.getAdapterPosition());
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (hasMultiStyle()) {
            return itemManager.getItemViewType(dataList.get(position), position);
        }
        return super.getItemViewType(position);
    }


    public void updateData(List<T> list) {
        if (list != null && list.size() > 0) {
            this.dataList.clear();
            this.dataList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<T> list) {
        if (list != null && list.size() > 0) {
            this.dataList.addAll(list);
            notifyDataSetChanged();
        }
    }
}
